#ifndef _TSOCKET_H_
#define _TSOCKET_H_
#include "tlogger.h"
#include "tbuffer.h"
#include "tnocopyable.h"
extern tlib::TBufferPool* g_bufferPool;
namespace tlib
{
	class TSocket : TNoCopyable
	{
		friend class TBuffer;
	public:
		static bool EnvInit();
		static void EnvFree();
		static int Send(int dstfd, char* buff, int len);
		static int Recv(int srcfd, char* buff, int len);
	public:
		TSocket();
		TSocket(int sockfd);
		virtual ~TSocket();
		bool Validate();
		int fd()
		{
			return sockfd_;
		};
		bool Listen(char* host, int port);
		TSocket* Accept();
		bool Connect(char* host, int port);
		int Send(char* buff, int len);
		int Recv(char* buff, int len);
		void Close();

		int WriteBuffer(int len);
		bool ReadBuffer(char* buff, int len);
		int GetReadAbleLen()
		{
			return buffer_->GetReadAbleLen();
		};
	public:
		bool SetNonBlock(bool on);
		bool SetTcpNoDelay(bool on);
		bool SetReuseAddr(bool on);
		bool SetReusePort(bool on);
		bool SetKeepAlive(bool on);
	private:
		int sockfd_;
		TBuffer* buffer_;
	};
}
#endif


